package com.jiang.demo.common.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.util.List;
import java.util.Map;

/**
 * ---------------------------------------------------------------------------------------------------------------
 * - 2024-06-01 15:24 - jiangshaoneng -
 * ---------------------------------------------------------------------------------------------------------------
 * -      ██╗██╗ █████╗ ███╗   ██╗ ██████╗ ███████╗██╗  ██╗ █████╗  ██████╗ ███╗   ██╗███████╗███╗   ██╗ ██████╗
 * -      ██║██║██╔══██╗████╗  ██║██╔════╝ ██╔════╝██║  ██║██╔══██╗██╔═══██╗████╗  ██║██╔════╝████╗  ██║██╔════╝
 * -      ██║██║███████║██╔██╗ ██║██║  ███╗███████╗███████║███████║██║   ██║██╔██╗ ██║█████╗  ██╔██╗ ██║██║  ███╗
 * - ██   ██║██║██╔══██║██║╚██╗██║██║   ██║╚════██║██╔══██║██╔══██║██║   ██║██║╚██╗██║██╔══╝  ██║╚██╗██║██║   ██║
 * - ╚█████╔╝██║██║  ██║██║ ╚████║╚██████╔╝███████║██║  ██║██║  ██║╚██████╔╝██║ ╚████║███████╗██║ ╚████║╚██████╔╝
 * -  ╚════╝ ╚═╝╚═╝  ╚═╝╚═╝  ╚═══╝ ╚═════╝ ╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═══╝╚══════╝╚═╝  ╚═══╝ ╚═════╝
 * ---------------------------------------------------------------------------------------------------------------
 * ---------------------------------------------------------------------------------------------------------------
 */
public class JsonUtil {

    public static ObjectMapper DEFAULT_OBJECT_MAPPER = new ObjectMapper();
    static {
        // 对于空的对象转json的时候不抛出错误
        DEFAULT_OBJECT_MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        // 序列化日期为timestamps
        DEFAULT_OBJECT_MAPPER.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        // 禁用遇到未知属性抛出异常
        DEFAULT_OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        // 不输出为null值的属性
        DEFAULT_OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    public static <T> T map2Object(Map<?, ?> map, Class<T> cls) {
        return DEFAULT_OBJECT_MAPPER.convertValue(map, cls);
    }

    public static <T> T map2Object(Map<?, ?> map, TypeReference<T> tr) {
        return DEFAULT_OBJECT_MAPPER.convertValue(map, tr);
    }

    public static <T> List<T> json2List(String json, Class<T> cls) throws JsonProcessingException {
        return DEFAULT_OBJECT_MAPPER.readValue(json, new TypeReference<List<T>>() {});
    }

    public static <T> T json2Object(String json, Class<T> cls) throws JsonProcessingException {
        return DEFAULT_OBJECT_MAPPER.readValue(json, cls);
    }

    public static <T> T json2Object(String json, TypeReference<T> tr) throws JsonProcessingException {
        return DEFAULT_OBJECT_MAPPER.readValue(json, tr);
    }

    public static String object2Json(Object o) throws JsonProcessingException {
        return DEFAULT_OBJECT_MAPPER.writeValueAsString(o);
    }

    public static Map<?, ?> json2Map(String json) throws JsonProcessingException {
        return json2Object(json, Map.class);
    }

    public static Map<?, ?> object2Map(Object o) throws JsonProcessingException {
        return json2Map(object2Json(o));
    }

}
